/**
 * 算式类 - 实验内容1
 * 设计算式类的成员变量、成员函数
 */
public class Expression {
    private int left;
    private int right;
    private String operator;
    
    public Expression(int left, int right, String operator) {
        this.left = left;
        this.right = right;
        this.operator = operator;
    }
    
    public int calculate() {
        if ("+".equals(operator)) {
            return left + right;
        } else {
            // 确保减法结果非负
            if (left < right) {
                int temp = left;
                left = right;
                right = temp;
            }
            return left - right;
        }
    }
    
    public String getExpression() {
        return left + " " + operator + " " + right + " = ";
    }
    
    public boolean checkAnswer(int answer) {
        return calculate() == answer;
    }
    
    public String toString() {
        return getExpression() + calculate();
    }
    
    // Getter方法
    public int getLeft() { return left; }
    public int getRight() { return right; }
    public String getOperator() { return operator; }
}